/**
 * @file testCSV.c
 * @brief 用于BookType的单元测试
 * @version 0.1
 * @date 2022-05-13
 *
 * @copyright Copyright (c) 2022
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include "../include/ListType.h"
#include "../include/BookType.h"
#include "../include/StrFunc.h"
#include "../include/AccountType.h"
int main()
{
    BookListType *bookList = BookListType_BookListType("./书本.csv");
    AccountListType *accountList = AccountListType_AccountListType("./账户.csv");
    printf("书名：");
    char *bookTitle = Str_lineFromStdin();
    printf("用户：");
    char *userName = Str_lineFromStdin();
    ListType *subList = BookListType_query(bookList, bookTitle);
    if (subList->size)
    {
        AccountType *account = AccountType_ListNode(AccountListType_query(accountList, userName));
        if (account)
        {
            BookType *book = BookType_ListNode(subList->head);
            if (book->currentQuantity)
            {
                if (account->count < account->limit)
                {
                    book->currentQuantity--;
                    printf("%s 借出一本，剩余库存 %d/%d \n", book->title, book->currentQuantity, book->totalQuantity);
                    account->count++;
                    printf("%s 借到一本，已借阅量 %d/%d\n", account->name, account->count, account->limit);
                }
                else
                {
                    printf("用户\"%s\"已经达到借书上限\n", userName);
                }
            }
            else
            {
                printf("%s %d/%d 已经全部借出\n", book->title, book->currentQuantity, book->totalQuantity);
            }
        }
        else
        {
            printf("不存在用户\"%s\"\n", userName);
        }
    }
    else
    {
        printf("没有关于\"%s\"的书\n", bookTitle);
    }
    BookListType_write(bookList);
    AccountListType_write(accountList);

    free(bookTitle);
    free(userName);
    BookListType_free(bookList);
    AccountListType_free(accountList);
    return 0;
}
